package com.lanxin.bx.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.lanxin.bx.entity.BxInformal;
import com.lanxin.bx.entity.BxInformalaccessary;
import com.lanxin.bx.entity.BxInformaldetail;
import com.lanxin.bx.entity.BxInformaltype;
import com.lanxin.bx.service.IBxInformalService;
import com.lanxin.bx.service.IBxInformalaccessaryService;
import com.lanxin.bx.service.IBxInformaldetailService;
import com.lanxin.bx.service.IBxInformaltypeService;
import com.lanxin.bx.vo.AllinformalVo;
import com.lanxin.hr.entity.ZzAccount;
import com.lanxin.hr.entity.ZzDepartment;
import com.lanxin.hr.entity.ZzEmployee;
import com.lanxin.hr.service.IZzDepartmentService;
import com.lanxin.hr.service.IZzEmployeeService;
import com.lanxin.judge.entity.LcFlow;
import com.lanxin.judge.service.ILcFlowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.time.ZoneId;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 
 * @since 2025-07-18
 */
@Controller
@RequestMapping("/bx-informaldetail")
public class BxInformaldetailController {
    @Autowired
    IBxInformalService bxInformalService;
    @Autowired
    IBxInformaldetailService bxInformaldetailService;
    @Autowired
    IBxInformalaccessaryService bxInformalaccessaryService;
    @Autowired
    IBxInformaltypeService bxInformaltypeService;
    @Autowired
    ILcFlowService lcFlowService;
    @Autowired
    IZzEmployeeService employeeService;
    @Autowired
    IZzDepartmentService departmentService;


    @RequestMapping("/bxInformalInfo")
    public ModelAndView bxInformalInfo(
            @RequestParam(value = "oid") Integer oid) {

        ModelAndView modelAndView = new ModelAndView();
        QueryWrapper<BxInformal> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("oid", oid);
        BxInformal bxInformal = bxInformalService.getOne(queryWrapper);
        // 转换 createDate（LocalDateTime -> Date）
        if (bxInformal.getCreateDate() != null) {
            Date createDate = Date.from(bxInformal.getCreateDate().atZone(ZoneId.systemDefault()).toInstant());
            modelAndView.addObject("createDate", createDate); // 传递转换后的 Date 对象
        }

// 转换 submitDate
        if (bxInformal.getSubmitDate() != null) {
            Date submitDate = Date.from(bxInformal.getSubmitDate().atZone(ZoneId.systemDefault()).toInstant());
            modelAndView.addObject("submitDate", submitDate);
        }

// 转换 bookDate
        if (bxInformal.getBookDate() != null) {
            Date bookDate = Date.from(bxInformal.getBookDate().atZone(ZoneId.systemDefault()).toInstant());
            modelAndView.addObject("bookDate", bookDate);
        }

        QueryWrapper<BxInformaldetail> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("parentid", oid);
        List<BxInformaldetail> bxInformaldetails = bxInformaldetailService.list(queryWrapper1);
        List<BxInformaltype> bxTypes = bxInformaltypeService.list(); // 需注入 BxTypeService
        Map<Integer, String> typeMap = new HashMap<>();
        for (BxInformaltype type : bxTypes) {
            typeMap.put(type.getOid(), type.getName()); // 类型ID作为key，名称作为value
        }
        modelAndView.addObject("typeMap", typeMap); // 传递映射到前端



        // 2.1 审批流程名称
        if (bxInformal.getFlowid() != null) {
            LcFlow lcFlow = lcFlowService.getById(bxInformal.getFlowid());
            modelAndView.addObject("flowName", lcFlow != null ? lcFlow.getFlowName() : ""); // 假设LcFlow有flowName字段
        }

        // 2.2 创建人名称
        if (bxInformal.getCreator() != null) {
            ZzEmployee creator = employeeService.getById(bxInformal.getCreator());
            modelAndView.addObject("creatorName", creator != null ? creator.getEmpName() : "");
        }

        // 2.3 申请人名称
        if (bxInformal.getProposer() != null) {
            ZzEmployee proposer = employeeService.getById(bxInformal.getProposer());
            modelAndView.addObject("proposerName", proposer != null ? proposer.getEmpName() : "");
        }

        // 2.4 部门名称
        if (bxInformal.getDeptid() != null) {
            ZzDepartment dept = departmentService.getById(bxInformal.getDeptid()); // 需注入deptService
            modelAndView.addObject("deptName", dept != null ? dept.getDeptName() : "");
        }

        // 2.5 领款人名称
        if (bxInformal.getPayee() != null) {
            ZzEmployee payee = employeeService.getById(bxInformal.getPayee());
            modelAndView.addObject("payeeName", payee != null ? payee.getEmpName() : "");
        }

        // 2.6 领款登记人名称
        if (bxInformal.getBooker() != null) {
            ZzEmployee booker = employeeService.getById(bxInformal.getBooker());
            modelAndView.addObject("bookerName", booker != null ? booker.getEmpName() : "");
        }


        QueryWrapper<BxInformalaccessary> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("parentid", oid);
        List<BxInformalaccessary> bxInformalaccessaries = bxInformalaccessaryService.list(queryWrapper2);

        modelAndView.addObject("bxInformal", bxInformal);
        modelAndView.addObject("bxInformaldetails", bxInformaldetails);
        modelAndView.addObject("bxInformalaccessaries", bxInformalaccessaries);
        modelAndView.setViewName("bx/allinformal/bxinformalinfo");

        return modelAndView;
    }

    @RequestMapping("/toPayeeInformal")
    public ModelAndView toPayeeInformal(
            @RequestParam(value = "oid") Integer oid) {

        ModelAndView modelAndView = new ModelAndView();
        QueryWrapper<BxInformal> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("oid", oid);
        BxInformal bxInformal = bxInformalService.getOne(queryWrapper);
        // 转换 createDate（LocalDateTime -> Date）
        if (bxInformal.getCreateDate() != null) {
            Date createDate = Date.from(bxInformal.getCreateDate().atZone(ZoneId.systemDefault()).toInstant());
            modelAndView.addObject("createDate", createDate); // 传递转换后的 Date 对象
        }

// 转换 submitDate
        if (bxInformal.getSubmitDate() != null) {
            Date submitDate = Date.from(bxInformal.getSubmitDate().atZone(ZoneId.systemDefault()).toInstant());
            modelAndView.addObject("submitDate", submitDate);
        }

// 转换 bookDate
        if (bxInformal.getBookDate() != null) {
            Date bookDate = Date.from(bxInformal.getBookDate().atZone(ZoneId.systemDefault()).toInstant());
            modelAndView.addObject("bookDate", bookDate);
        }

        QueryWrapper<BxInformaldetail> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("parentid", oid);
        List<BxInformaldetail> bxInformaldetails = bxInformaldetailService.list(queryWrapper1);
        List<BxInformaltype> bxTypes = bxInformaltypeService.list(); // 需注入 BxTypeService
        Map<Integer, String> typeMap = new HashMap<>();
        for (BxInformaltype type : bxTypes) {
            typeMap.put(type.getOid(), type.getName()); // 类型ID作为key，名称作为value
        }
        modelAndView.addObject("typeMap", typeMap); // 传递映射到前端



        // 2.1 审批流程名称
        if (bxInformal.getFlowid() != null) {
            LcFlow lcFlow = lcFlowService.getById(bxInformal.getFlowid());
            modelAndView.addObject("flowName", lcFlow != null ? lcFlow.getFlowName() : ""); // 假设LcFlow有flowName字段
        }

        // 2.2 创建人名称
        if (bxInformal.getCreator() != null) {
            ZzEmployee creator = employeeService.getById(bxInformal.getCreator());
            modelAndView.addObject("creatorName", creator != null ? creator.getEmpName() : "");
        }

        // 2.3 申请人名称
        if (bxInformal.getProposer() != null) {
            ZzEmployee proposer = employeeService.getById(bxInformal.getProposer());
            modelAndView.addObject("proposerName", proposer != null ? proposer.getEmpName() : "");
        }

        // 2.4 部门名称
        if (bxInformal.getDeptid() != null) {
            ZzDepartment dept = departmentService.getById(bxInformal.getDeptid()); // 需注入deptService
            modelAndView.addObject("deptName", dept != null ? dept.getDeptName() : "");
        }

        // 2.5 领款人名称
        if (bxInformal.getPayee() != null) {
            ZzEmployee payee = employeeService.getById(bxInformal.getPayee());
            modelAndView.addObject("payeeName", payee != null ? payee.getEmpName() : "");
        }

        // 2.6 领款登记人名称
        if (bxInformal.getBooker() != null) {
            ZzEmployee booker = employeeService.getById(bxInformal.getBooker());
            modelAndView.addObject("bookerName", booker != null ? booker.getEmpName() : "");
        }


        QueryWrapper<BxInformalaccessary> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("parentid", oid);
        List<BxInformalaccessary> bxInformalaccessaries = bxInformalaccessaryService.list(queryWrapper2);
        QueryWrapper<ZzEmployee> employeeQueryWrapper = new QueryWrapper<>();
        employeeQueryWrapper.eq("oid",bxInformal.getProposer());
        ZzEmployee employee=employeeService.getOne(employeeQueryWrapper);
        modelAndView.addObject("employee", employee);
        modelAndView.addObject("bxInformal", bxInformal);
        modelAndView.addObject("bxInformaldetails", bxInformaldetails);
        modelAndView.addObject("bxInformalaccessaries", bxInformalaccessaries);
        modelAndView.setViewName("bx/allinformal/payeebxinformal");

        return modelAndView;
    }




}
